package game.metalmax.animation
{
	import flash.geom.Point;
	
	import game.metalmax.config.GameConfig;
	
	import showtime.framework.engine.IInteraction;
	import showtime.framework.engine.Sprite2D;
	import showtime.framework.engine.animation.Animation;
	import showtime.framework.engine.config.AnimationConfig;
	import showtime.framework.events.GameEvent;
	import showtime.framework.events.MessageDispatcher;
	import showtime.framework.manager.AssetManager;
	import showtime.framework.manager.DataBaseManager;
	import showtime.framework.object.DynamicModelObject;
	
	
	public class DynamicSOB extends Sprite2D implements IInteraction
	{
		public var lastDirection:int = 0;
		
		public var running:Boolean = false;	

		public var targetX:int = 0;
		public var targetY:int = 0;
		
		public var source:DynamicModelObject = null;
		
		public function DynamicSOB(source:DynamicModelObject)
		{
			this.source = source;
			
			configAvatar();

			x = source.spawnPoint.x;
			y = source.spawnPoint.y;
			
			direction = source.direction;
			lastDirection = direction;
			
			targetX = x;
			targetY = y;

		}
				
		override public function destroy():void
		{
			super.destroy();
			
			source = null;	
		}
		
		override public function play(animationName:String, force:Boolean = false):void
		{
			if (!running || force && source.animationName != animationName)
			{
				super.play(animationName, force);
				source.animationName = animationName;
			}
		}
		
		override public function update():void
		{
			super.update();
			
			if (running)
			{
				if (targetX == x && targetY == y)
				{
					running = false;
					return;
				}				
				
				var dx:Number = targetX - x;
				var dy:Number = targetY - y;
				
				var distance:int = Math.sqrt(dx * dx + dy * dy);
				
				if (distance < source.velocity)
				{
					x = targetX;
					y = targetY;
					
					velocity.x = 0;
					velocity.y = 0;
					
					running = false;

					moveStop();					
				}
				
				else
				{
					angle = Math.atan2(dy, dx);
					
					velocity.x = source.velocity * Math.cos(angle);
					velocity.y = source.velocity * Math.sin(angle);
				
				}
			}
		}
		
		override public function render():void
		{
			super.render();				
		}
		
		public function configAI():void
		{
			
		}
		
		public function configAvatar():void
		{
			loadGraphic(source.graphicID, AssetManager.getInstance().getGraphic(source.graphicID), false, GameConfig.MAP_TILE_WIDTH, GameConfig.MAP_TILE_HEIGHT);
			 
			var animationConfig:AnimationConfig = DataBaseManager.getInstance().read(AnimationConfig, source.animationConfigID);
			
			animations.length = 0;
			for each (var animation:Animation in animationConfig.animations)
			{
				addAnimation(animation.name, animation.frames, animation.frameRate, animation.looped);
			}
			
			//play(source.animationName);
		}
		
		public function moveStop():void
		{			
			MessageDispatcher.getInstance().dispatchEvent(new GameEvent(GameEvent.OBJECT_MOVE_STOP, source));
		}
		
		public function moveTo(direction:int):void
		{		
			MessageDispatcher.getInstance().dispatchEvent(new GameEvent(GameEvent.OBJECT_MOVE_START, source));			
		}	
		
		public function getAnimationName():String
		{
			return nowAnimation != null ? nowAnimation.name : null;
		}
		
		public function getOppositeAnimationName():String
		{
			if (getAnimationName().indexOf("BOTTOM") != -1)
			{
				return getAnimationName().replace("BOTTOM", "TOP").replace("ACTIVE", "IDLE");
			}
			else if (getAnimationName().indexOf("TOP") != -1)
			{
				return getAnimationName().replace("TOP", "BOTTOM").replace("ACTIVE", "IDLE");
			}
			else if (getAnimationName().indexOf("LEFT") != -1)
			{
				return getAnimationName().replace("LEFT", "RIGHT").replace("ACTIVE", "IDLE");
			}
			else
			{
				return getAnimationName().replace("RIGHT", "LEFT").replace("ACTIVE", "IDLE");
			}
		}		
		
		public function getNowPoint():Point
		{
			return new Point(x, y);
		}
		
		
		public function click():void
		{
			
		}
		
		public function rollOver():void
		{
			
		}
		
		public function rollOut():void
		{

		}
	}
}